Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
Visual Basic 6 - [help] creare funzione MD5??
Forum - Visual Basic 6 - [help] creare funzione MD5??

Pagine: [ 1 2 ] Precedente | Prossimo
Avatar
sub0 (Ex-Member)
Rookie


Messaggi: 56
Iscritto: 05/10/2009

Segnala al moderatore
Postato alle 22:41
Venerdì, 27/11/2009
Salve a tutti. Questa sera volevo rompervi con una domanda. In VB6 come faccio a creare una funzione per calcolare l'hash MD5 di una stringa? Ho cercato un pò sul WorldWideWeb però non ho trovato quello che cerco. Mi potete aiutare? Thanks a lot :D:D:hail::hail:

Perchè voglio sapere questo?
Sto creando un client MSN. E fin qui tutto ok. Il problema è che ogni tot tempo il server di Messenger mi invia una stringa per verificare che io sia attivo e per altre cose (Challenge). La stringa è tipo (esempio) "CHL 0 46652358383458". Io a questa stringa aggiungo "Q1P7W2E4J9R8U3S5" e quindi viene "46652358383458Q1P7W2E4J9R8U3S5". Questa stringa, prima di reinviarla al server la devo criptare con MD5. Il tutto deve avvenire in 50 secondi altrimenti il server mi killa la connessione. Tutto qui xD


Sub0

PM Quote
Avatar
GrG (Member)
Guru^2


Messaggi: 3430
Iscritto: 21/08/2007

Segnala al moderatore
Postato alle 12:40
Sabato, 28/11/2009
Project -> Add Class Module

e dentro ci metti:
Codice sorgente - presumibilmente VB.NET

  1. Option Explicit
  2.  
  3.  
  4. Private Const OFFSET_4 = 4294967296#
  5. Private Const MAXINT_4 = 2147483647
  6.  
  7. Private Const S11 = 7
  8. Private Const S12 = 12
  9. Private Const S13 = 17
  10. Private Const S14 = 22
  11. Private Const S21 = 5
  12. Private Const S22 = 9
  13. Private Const S23 = 14
  14. Private Const S24 = 20
  15. Private Const S31 = 4
  16. Private Const S32 = 11
  17. Private Const S33 = 16
  18. Private Const S34 = 23
  19. Private Const S41 = 6
  20. Private Const S42 = 10
  21. Private Const S43 = 15
  22. Private Const S44 = 21
  23.  
  24.  
  25. '=
  26. '= Class Variables
  27. '=
  28. Private State(4) As Long
  29. Private ByteCounter As Long
  30. Private ByteBuffer(63) As Byte
  31.  
  32.  
  33. '=
  34. '= Class Properties
  35. '=
  36. Property Get RegisterA() As String
  37.     RegisterA = State(1)
  38. End Property
  39.  
  40. Property Get RegisterB() As String
  41.     RegisterB = State(2)
  42. End Property
  43.  
  44. Property Get RegisterC() As String
  45.     RegisterC = State(3)
  46. End Property
  47.  
  48. Property Get RegisterD() As String
  49.     RegisterD = State(4)
  50. End Property
  51.  
  52.  
  53. '=
  54. '= Class Functions
  55. '=
  56.  
  57. '
  58. ' Function to quickly digest a file into a hex string
  59. '
  60. Public Function DigestFileToHexStr(FileName As String) As String
  61.     Open FileName For Binary Access Read As #1
  62.     MD5Init
  63.     Do While Not EOF(1)
  64.         Get #1, , ByteBuffer
  65.         If Loc(1) < LOF(1) Then
  66.             ByteCounter = ByteCounter + 64
  67.             MD5Transform ByteBuffer
  68.         End If
  69.     Loop
  70.     ByteCounter = ByteCounter + (LOF(1) Mod 64)
  71.     Close #1
  72.     MD5Final
  73.     DigestFileToHexStr = GetValues
  74. End Function
  75.  
  76. '
  77. ' Function to digest a text string and output the result as a string
  78. ' of hexadecimal characters.
  79. '
  80. Public Function DigestStrToHexStr(SourceString As String) As String
  81.     MD5Init
  82.     MD5Update Len(SourceString), StringToArray(SourceString)
  83.     MD5Final
  84.     DigestStrToHexStr = GetValues
  85. End Function
  86.  
  87. '
  88. ' A utility function which converts a string into an array of
  89. ' bytes.
  90. '
  91. Private Function StringToArray(InString As String) As Byte()
  92.     Dim I As Integer
  93.     Dim bytBuffer() As Byte
  94.     ReDim bytBuffer(Len(InString))
  95.     For I = 0 To Len(InString) - 1
  96.         bytBuffer(I) = Asc(Mid(InString, I + 1, 1))
  97.     Next I
  98.     StringToArray = bytBuffer
  99. End Function
  100.  
  101. '
  102. ' Concatenate the four state vaules into one string
  103. '
  104. Public Function GetValues() As String
  105.     GetValues = LongToString(State(1)) & LongToString(State(2)) & LongToString(State(3)) & LongToString(State(4))
  106. End Function
  107.  
  108. '
  109. ' Convert a Long to a Hex string
  110. '
  111. Private Function LongToString(Num As Long) As String
  112.         Dim a As Byte
  113.         Dim b As Byte
  114.         Dim c As Byte
  115.         Dim d As Byte
  116.        
  117.         a = Num And &HFF&
  118.         If a < 16 Then
  119.             LongToString = "0" & Hex(a)
  120.         Else
  121.             LongToString = Hex(a)
  122.         End If
  123.                
  124.         b = (Num And &HFF00&) \ 256
  125.         If b < 16 Then
  126.             LongToString = LongToString & "0" & Hex(b)
  127.         Else
  128.             LongToString = LongToString & Hex(b)
  129.         End If
  130.        
  131.         c = (Num And &HFF0000) \ 65536
  132.         If c < 16 Then
  133.             LongToString = LongToString & "0" & Hex(c)
  134.         Else
  135.             LongToString = LongToString & Hex(c)
  136.         End If
  137.        
  138.         If Num < 0 Then
  139.             d = ((Num And &H7F000000) \ 16777216) Or &H80&
  140.         Else
  141.             d = (Num And &HFF000000) \ 16777216
  142.         End If
  143.        
  144.         If d < 16 Then
  145.             LongToString = LongToString & "0" & Hex(d)
  146.         Else
  147.             LongToString = LongToString & Hex(d)
  148.         End If
  149.    
  150. End Function
  151.  
  152. '
  153. ' Initialize the class
  154. '   This must be called before a digest calculation is started
  155. '
  156. Public Sub MD5Init()
  157.     ByteCounter = 0
  158.     State(1) = UnsignedToLong(1732584193#)
  159.     State(2) = UnsignedToLong(4023233417#)
  160.     State(3) = UnsignedToLong(2562383102#)
  161.     State(4) = UnsignedToLong(271733878#)
  162. End Sub
  163.  
  164. '
  165. ' MD5 Final
  166. '
  167. Public Sub MD5Final()
  168.     Dim dblBits As Double
  169.    
  170.     Dim padding(72) As Byte
  171.     Dim lngBytesBuffered As Long
  172.    
  173.     padding(0) = &H80
  174.    
  175.     dblBits = ByteCounter * 8
  176.    
  177.     ' Pad out
  178.     lngBytesBuffered = ByteCounter Mod 64
  179.     If lngBytesBuffered <= 56 Then
  180.         MD5Update 56 - lngBytesBuffered, padding
  181.     Else
  182.         MD5Update 120 - ByteCounter, padding
  183.     End If
  184.    
  185.    
  186.     padding(0) = UnsignedToLong(dblBits) And &HFF&
  187.     padding(1) = UnsignedToLong(dblBits) \ 256 And &HFF&
  188.     padding(2) = UnsignedToLong(dblBits) \ 65536 And &HFF&
  189.     padding(3) = UnsignedToLong(dblBits) \ 16777216 And &HFF&
  190.     padding(4) = 0
  191.     padding(5) = 0
  192.     padding(6) = 0
  193.     padding(7) = 0
  194.    
  195.     MD5Update 8, padding
  196. End Sub
  197.  
  198. '
  199. ' Break up input stream into 64 byte chunks
  200. '
  201. Public Sub MD5Update(InputLen As Long, InputBuffer() As Byte)
  202.     Dim II As Integer
  203.     Dim I As Integer
  204.     Dim J As Integer
  205.     Dim K As Integer
  206.     Dim lngBufferedBytes As Long
  207.     Dim lngBufferRemaining As Long
  208.     Dim lngRem As Long
  209.    
  210.     lngBufferedBytes = ByteCounter Mod 64
  211.     lngBufferRemaining = 64 - lngBufferedBytes
  212.     ByteCounter = ByteCounter + InputLen
  213.     ' Use up old buffer results first
  214.     If InputLen >= lngBufferRemaining Then
  215.         For II = 0 To lngBufferRemaining - 1
  216.             ByteBuffer(lngBufferedBytes + II) = InputBuffer(II)
  217.         Next II
  218.         MD5Transform ByteBuffer
  219.        
  220.         lngRem = (InputLen) Mod 64
  221.         ' The transfer is a multiple of 64 lets do some transformations
  222.         For I = lngBufferRemaining To InputLen - II - lngRem Step 64
  223.             For J = 0 To 63
  224.                 ByteBuffer(J) = InputBuffer(I + J)
  225.             Next J
  226.             MD5Transform ByteBuffer
  227.         Next I
  228.         lngBufferedBytes = 0
  229.     Else
  230.       I = 0
  231.     End If
  232.    
  233.     ' Buffer any remaining input
  234.     For K = 0 To InputLen - I - 1
  235.         ByteBuffer(lngBufferedBytes + K) = InputBuffer(I + K)
  236.     Next K
  237.    
  238. End Sub
  239.  
  240. '
  241. ' MD5 Transform
  242. '
  243. Private Sub MD5Transform(Buffer() As Byte)
  244.     Dim x(16) As Long
  245.     Dim a As Long
  246.     Dim b As Long
  247.     Dim c As Long
  248.     Dim d As Long
  249.    
  250.     a = State(1)
  251.     b = State(2)
  252.     c = State(3)
  253.     d = State(4)
  254.    
  255.     Decode 64, x, Buffer
  256.  
  257.     ' Round 1
  258.     FF a, b, c, d, x(0), S11, -680876936
  259.     FF d, a, b, c, x(1), S12, -389564586
  260.     FF c, d, a, b, x(2), S13, 606105819
  261.     FF b, c, d, a, x(3), S14, -1044525330
  262.     FF a, b, c, d, x(4), S11, -176418897
  263.     FF d, a, b, c, x(5), S12, 1200080426
  264.     FF c, d, a, b, x(6), S13, -1473231341
  265.     FF b, c, d, a, x(7), S14, -45705983
  266.     FF a, b, c, d, x(8), S11, 1770035416
  267.     FF d, a, b, c, x(9), S12, -1958414417
  268.     FF c, d, a, b, x(10), S13, -42063
  269.     FF b, c, d, a, x(11), S14, -1990404162
  270.     FF a, b, c, d, x(12), S11, 1804603682
  271.     FF d, a, b, c, x(13), S12, -40341101
  272.     FF c, d, a, b, x(14), S13, -1502002290
  273.     FF b, c, d, a, x(15), S14, 1236535329
  274.    
  275.     ' Round 2
  276.     GG a, b, c, d, x(1), S21, -165796510
  277.     GG d, a, b, c, x(6), S22, -1069501632
  278.     GG c, d, a, b, x(11), S23, 643717713
  279.     GG b, c, d, a, x(0), S24, -373897302
  280.     GG a, b, c, d, x(5), S21, -701558691
  281.     GG d, a, b, c, x(10), S22, 38016083
  282.     GG c, d, a, b, x(15), S23, -660478335
  283.     GG b, c, d, a, x(4), S24, -405537848
  284.     GG a, b, c, d, x(9), S21, 568446438
  285.     GG d, a, b, c, x(14), S22, -1019803690
  286.     GG c, d, a, b, x(3), S23, -187363961
  287.     GG b, c, d, a, x(8), S24, 1163531501
  288.     GG a, b, c, d, x(13), S21, -1444681467
  289.     GG d, a, b, c, x(2), S22, -51403784
  290.     GG c, d, a, b, x(7), S23, 1735328473
  291.     GG b, c, d, a, x(12), S24, -1926607734
  292.    
  293.     ' Round 3
  294.     HH a, b, c, d, x(5), S31, -378558
  295.     HH d, a, b, c, x(8), S32, -2022574463
  296.     HH c, d, a, b, x(11), S33, 1839030562
  297.     HH b, c, d, a, x(14), S34, -35309556
  298.     HH a, b, c, d, x(1), S31, -1530992060
  299.     HH d, a, b, c, x(4), S32, 1272893353
  300.     HH c, d, a, b, x(7), S33, -155497632
  301.     HH b, c, d, a, x(10), S34, -1094730640
  302.     HH a, b, c, d, x(13), S31, 681279174
  303.     HH d, a, b, c, x(0), S32, -358537222
  304.     HH c, d, a, b, x(3), S33, -722521979
  305.     HH b, c, d, a, x(6), S34, 76029189
  306.     HH a, b, c, d, x(9), S31, -640364487
  307.     HH d, a, b, c, x(12), S32, -421815835
  308.     HH c, d, a, b, x(15), S33, 530742520
  309.     HH b, c, d, a, x(2), S34, -995338651
  310.    
  311.     ' Round 4
  312.     II a, b, c, d, x(0), S41, -198630844
  313.     II d, a, b, c, x(7), S42, 1126891415
  314.     II c, d, a, b, x(14), S43, -1416354905
  315.     II b, c, d, a, x(5), S44, -57434055
  316.     II a, b, c, d, x(12), S41, 1700485571
  317.     II d, a, b, c, x(3), S42, -1894986606
  318.     II c, d, a, b, x(10), S43, -1051523
  319.     II b, c, d, a, x(1), S44, -2054922799
  320.     II a, b, c, d, x(8), S41, 1873313359
  321.     II d, a, b, c, x(15), S42, -30611744
  322.     II c, d, a, b, x(6), S43, -1560198380
  323.     II b, c, d, a, x(13), S44, 1309151649
  324.     II a, b, c, d, x(4), S41, -145523070
  325.     II d, a, b, c, x(11), S42, -1120210379
  326.     II c, d, a, b, x(2), S43, 718787259
  327.     II b, c, d, a, x(9), S44, -343485551
  328.    
  329.    
  330.     State(1) = LongOverflowAdd(State(1), a)
  331.     State(2) = LongOverflowAdd(State(2), b)
  332.     State(3) = LongOverflowAdd(State(3), c)
  333.     State(4) = LongOverflowAdd(State(4), d)
  334.  
  335. '  /* Zeroize sensitive information.
  336. '*/
  337. '  MD5_memset ((POINTER)x, 0, sizeof (x));
  338.    
  339. End Sub
  340.  
  341. Private Sub Decode(Length As Integer, OutputBuffer() As Long, InputBuffer() As Byte)
  342.     Dim intDblIndex As Integer
  343.     Dim intByteIndex As Integer
  344.     Dim dblSum As Double
  345.    
  346.     intDblIndex = 0
  347.     For intByteIndex = 0 To Length - 1 Step 4
  348.         dblSum = InputBuffer(intByteIndex) + _
  349.                                     InputBuffer(intByteIndex + 1) * 256# + _
  350.                                     InputBuffer(intByteIndex + 2) * 65536# + _
  351.                                     InputBuffer(intByteIndex + 3) * 16777216#
  352.         OutputBuffer(intDblIndex) = UnsignedToLong(dblSum)
  353.         intDblIndex = intDblIndex + 1
  354.     Next intByteIndex
  355. End Sub
  356.  
  357. '
  358. ' FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
  359. ' Rotation is separate from addition to prevent recomputation.
  360. '
  361. Private Function FF(a As Long, _
  362.                     b As Long, _
  363.                     c As Long, _
  364.                     d As Long, _
  365.                     x As Long, _
  366.                     s As Long, _
  367.                     ac As Long) As Long
  368.     a = LongOverflowAdd4(a, (b And c) Or (Not (b) And d), x, ac)
  369.     a = LongLeftRotate(a, s)
  370.     a = LongOverflowAdd(a, b)
  371. End Function
  372.  
  373. Private Function GG(a As Long, _
  374.                     b As Long, _
  375.                     c As Long, _
  376.                     d As Long, _
  377.                     x As Long, _
  378.                     s As Long, _
  379.                     ac As Long) As Long
  380.     a = LongOverflowAdd4(a, (b And d) Or (c And Not (d)), x, ac)
  381.     a = LongLeftRotate(a, s)
  382.     a = LongOverflowAdd(a, b)
  383. End Function
  384.  
  385. Private Function HH(a As Long, _
  386.                     b As Long, _
  387.                     c As Long, _
  388.                     d As Long, _
  389.                     x As Long, _
  390.                     s As Long, _
  391.                     ac As Long) As Long
  392.     a = LongOverflowAdd4(a, b Xor c Xor d, x, ac)
  393.     a = LongLeftRotate(a, s)
  394.     a = LongOverflowAdd(a, b)
  395. End Function
  396.  
  397. Private Function II(a As Long, _
  398.                     b As Long, _
  399.                     c As Long, _
  400.                     d As Long, _
  401.                     x As Long, _
  402.                     s As Long, _
  403.                     ac As Long) As Long
  404.     a = LongOverflowAdd4(a, c Xor (b Or Not (d)), x, ac)
  405.     a = LongLeftRotate(a, s)
  406.     a = LongOverflowAdd(a, b)
  407. End Function
  408.  
  409. '
  410. ' Rotate a long to the right
  411. '
  412. Function LongLeftRotate(value As Long, bits As Long) As Long
  413.     Dim lngSign As Long
  414.     Dim lngI As Long
  415.     bits = bits Mod 32
  416.     If bits = 0 Then LongLeftRotate = value: Exit Function
  417.     For lngI = 1 To bits
  418.         lngSign = value And &HC0000000
  419.         value = (value And &H3FFFFFFF) * 2
  420.         value = value Or ((lngSign < 0) And 1) Or (CBool(lngSign And _
  421.                 &H40000000) And &H80000000)
  422.     Next
  423.     LongLeftRotate = value
  424. End Function
  425.  
  426. '
  427. ' Function to add two unsigned numbers together as in C.
  428. ' Overflows are ignored!
  429. '
  430. Private Function LongOverflowAdd(Val1 As Long, Val2 As Long) As Long
  431.     Dim lngHighWord As Long
  432.     Dim lngLowWord As Long
  433.     Dim lngOverflow As Long
  434.  
  435.     lngLowWord = (Val1 And &HFFFF&) + (Val2 And &HFFFF&)
  436.     lngOverflow = lngLowWord \ 65536
  437.     lngHighWord = (((Val1 And &HFFFF0000) \ 65536) + ((Val2 And &HFFFF0000) \ 65536) + lngOverflow) And &HFFFF&
  438.     LongOverflowAdd = UnsignedToLong((lngHighWord * 65536#) + (lngLowWord And &HFFFF&))
  439. End Function
  440.  
  441. '
  442. ' Function to add two unsigned numbers together as in C.
  443. ' Overflows are ignored!
  444. '
  445. Private Function LongOverflowAdd4(Val1 As Long, Val2 As Long, val3 As Long, val4 As Long) As Long
  446.     Dim lngHighWord As Long
  447.     Dim lngLowWord As Long
  448.     Dim lngOverflow As Long
  449.  
  450.     lngLowWord = (Val1 And &HFFFF&) + (Val2 And &HFFFF&) + (val3 And &HFFFF&) + (val4 And &HFFFF&)
  451.     lngOverflow = lngLowWord \ 65536
  452.     lngHighWord = (((Val1 And &HFFFF0000) \ 65536) + _
  453.                    ((Val2 And &HFFFF0000) \ 65536) + _
  454.                    ((val3 And &HFFFF0000) \ 65536) + _
  455.                    ((val4 And &HFFFF0000) \ 65536) + _
  456.                    lngOverflow) And &HFFFF&
  457.     LongOverflowAdd4 = UnsignedToLong((lngHighWord * 65536#) + (lngLowWord And &HFFFF&))
  458. End Function
  459.  
  460. '
  461. ' Convert an unsigned double into a long
  462. '
  463. Private Function UnsignedToLong(value As Double) As Long
  464.         If value < 0 Or value >= OFFSET_4 Then Error 6 ' Overflow
  465.         If value <= MAXINT_4 Then
  466.           UnsignedToLong = value
  467.         Else
  468.           UnsignedToLong = value - OFFSET_4
  469.         End If
  470.       End Function
  471.  
  472. '
  473. ' Convert a long to an unsigned Double
  474. '
  475. Private Function LongToUnsigned(value As Long) As Double
  476.         If value < 0 Then
  477.           LongToUnsigned = value + OFFSET_4
  478.         Else
  479.           LongToUnsigned = value
  480.         End If
  481. End Function



Poi nella form_load metti ad esempio:
Codice sorgente - presumibilmente VB.NET

  1. Dim MD5 As Class1
  2. Set MD5 = New Class1
  3. MsgBox MD5.DigestStrToHexStr("ciao")


Ultima modifica effettuata da GrG il 28/11/2009 alle 12:48
PM Quote
Avatar
sub0 (Ex-Member)
Rookie


Messaggi: 56
Iscritto: 05/10/2009

Segnala al moderatore
Postato alle 16:08
Sabato, 28/11/2009
Grazie GrG. Ho fatto la stessa cosa che hai detto tu. L'ho trovata su internet poco dopo aver scritto il post :D Comunque ho imparato una cosa nuova. Grazie ancora e scusa per il disturbo

PM Quote
Avatar
GrG (Member)
Guru^2


Messaggi: 3430
Iscritto: 21/08/2007

Segnala al moderatore
Postato alle 17:02
Sabato, 28/11/2009
di niente ;)

comunque come vedi, prima di postare sul forum controlla bene su internet, che spesso le cose le trovi :)

PM Quote
Avatar
muteblaster (Member)
Pro


Messaggi: 87
Iscritto: 16/07/2009

Segnala al moderatore
Postato alle 11:28
Domenica, 29/11/2009
complimenti per il codice postato......
calcolare il codice hash senza richiamare le cryptoapi e' cosa tutt'altro che semplice e richiede un lavor non idifferente.....
bravo.....io non ci sarei riuscito

PM Quote
Avatar
GrG (Member)
Guru^2


Messaggi: 3430
Iscritto: 21/08/2007

Segnala al moderatore
Postato alle 11:55
Domenica, 29/11/2009
Testo quotato

Postato originariamente da muteblaster:

complimenti per il codice postato......
calcolare il codice hash senza richiamare le cryptoapi e' cosa tutt'altro che semplice e richiede un lavor non idifferente.....
bravo.....io non ci sarei riuscito



Lol, il codice non l'ho sviluppato io, neanche io ne sarei in grado, ma è stato sviluppato da un certo Robert Hubley che poi ha condiviso in internet...

PM Quote
Avatar
foralobo (Normal User)
Pro


Messaggi: 146
Iscritto: 30/11/2009

Segnala al moderatore
Postato alle 8:26
Mercoledì, 02/12/2009
evviva l'umiltà...:)

Ultima modifica effettuata da foralobo il 02/12/2009 alle 8:26
PM Quote
Avatar
doom94 (Normal User)
Rookie


Messaggi: 38
Iscritto: 07/12/2009

Segnala al moderatore
Postato alle 14:52
Domenica, 13/12/2009
Molto bello! Come potrei fare in modo di aggiungere al form textbox per inserire la parola da criptare?:love::love:

PM Quote
Avatar
GrG (Member)
Guru^2


Messaggi: 3430
Iscritto: 21/08/2007

Segnala al moderatore
Postato alle 16:41
Domenica, 13/12/2009
al posto di:

MsgBox MD5.DigestStrToHexStr("ciao")

metti:
dim variabile as string

variabile = MD5.DigestStrToHexStr(text1.text)

PM Quote
Pagine: [ 1 2 ] Precedente | Prossimo